package com.freesky.biz.icbc.controller;

import com.freesky.biz.icbc.domain.TAigouActivity;
import com.freesky.biz.icbc.domain.TAigouCategory;
import com.freesky.biz.icbc.service.IAigouActivityService;
import com.freesky.biz.icbc.service.IAigouCategoryService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.constant.UserConstants;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysDept;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.StringUtils;
import com.freesky.system.service.ISysDeptService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工行爱购活动类别管理
 */
@RestController
@RequestMapping("/icbc/aigou/category")
public class AiguoCategoryController extends BaseController {

    @Autowired
    private IAigouCategoryService aigouCategoryService;

    /**
     * 获取部门列表
     */
    @PreAuthorize("@ss.hasPermi('icbc:aigoucategory:list')")
    @GetMapping("/list")
    public AjaxResult list(TAigouCategory entity) {
        List<TAigouCategory> depts = aigouCategoryService.selectCategoryList(entity);
        return success(depts);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('icbc:aigoucategory:list')")
    @GetMapping("/list/exclude/{deptId}")
    public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) String deptId)
    {
        List<TAigouCategory> depts = aigouCategoryService.selectCategoryList(new TAigouCategory());
        depts.removeIf(d -> d.getCategoryId().equalsIgnoreCase(deptId) || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId));
        return success(depts);
    }

    @PreAuthorize("@ss.hasPermi('icbc:aigoucategory:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable String deptId)
    {
        return success(aigouCategoryService.selectCategoryById(deptId));
    }

    /**
     * 新增部门
     */
    @PreAuthorize("@ss.hasPermi('icbc:aigoucategory:add')")
    @Log(title = "工行爱购类别管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TAigouCategory entity)
    {
        entity.setCreateBy(getUsername());
        return toAjax(aigouCategoryService.insertCategory(entity));
    }


    @PreAuthorize("@ss.hasPermi('icbc:aigoucategory:edit')")
    @Log(title = "工行爱购类别管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TAigouCategory entity)
    {
        String deptId = entity.getCategoryId();
        if (entity.getParentId().equals(deptId))
        {
            return error("修改栏目'" + entity.getCategoryName() + "'失败，上级栏目不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, entity.getStatus()) && aigouCategoryService.selectNormalChildrenCategoryById(deptId) > 0)
        {
            return error("该栏目包含未停用的子部门！");
        }
        return toAjax(aigouCategoryService.updateCategory(entity));
    }


    @PreAuthorize("@ss.hasPermi('icbc:aigoucategory:remove')")
    @Log(title = "工行爱购类别管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable String deptId)
    {
        if (aigouCategoryService.hasChildByCategoryId(deptId))
        {
            return warn("存在下级类别,不允许删除");
        }
        return toAjax(aigouCategoryService.deleteCategoryById(deptId));
    }

}
